<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
class DCRecordationAndTransferTaxCalculator{

    private $rateInfoProperties;
    private $titleInsuranceCalculatorHelper;
    private $state;
    private $countyLocales;
    private $taxCalculatorHelper;
    private $feeCalculator;

    public function DCRecordationAndTransferTaxCalculator($state, $rateInfoProperties) 
    {
       $this->state = $state;
       $this->rateInfoProperties = $rateInfoProperties;
       
       $this->titleInsuranceCalculatorHelper = new TitleInsuranceCalculatorHelper();
       
       $this->taxCalculatorHelper = new TaxCalculatorHelper();
       
       $this->countyLocales = CountyLocale::getCountyLocales($state);
       $this->feeCalculator = new FeeCalculator($state, $rateInfoProperties);
    }    

    public function supportsCountyLocale() {
        return true;
    }

    public function getCountyLocales() {
        return $this->countyLocales;
    }

    public function supportsTownLocale() {
        return false;
    }

    public function getTownLocales($countyLocale) {
        return array();
    }

    public function calculatePurchaseRecordationAndTransferTax($countyLocale,$townLocale,$consideration,$loanAmounts,$qualifiedFirstTimeBuyer,$ownerOccupiedResidence,$requestedPayer) 
    {

        $recordationTax = $this->getRecordationTax($consideration);
        $propertyTransferTax = $this->getStatePropertyTransferTax($consideration);

        // zero indicates false
        $mortgage = $this->taxCalculatorHelper->getNumberOfLoans($loanAmounts);

        // CT-32 Added fees per State
        $fees = $this->feeCalculator->calculate(true, $mortgage, $ownerOccupiedResidence);

        return RecordationAndTransferTaxCreator::createSplitRecordationAndTransferTaxes($this->state, $propertyTransferTax, null, $recordationTax,
                0.0,$fees,$requestedPayer, null);
    }

    public function getHeaderHTML() {
        return "Purchase/sales transactions in DC are subject to transfer and recordation taxes. Generally, refinance transactions are not subject to any tax. However, DC does charge recording fees for all transactions.";
    }

    public function getFooterHTML() {
        return null;
    }

    public function calculateRefinanceRecordationAndTransferTax($countyLocale, $loanAmounts, $ownerOccupiedResidence,$payoffAmounts,$sameLender) {

        // No taxes in refi in dc
        return new RecordationAndTransferTaxes(
                null, null, null,
                $this->feeCalculator->calculate(false, count($loanAmounts), $ownerOccupiedResidence),
                null);
    }

    protected function getRecordationTax($consideration) {
        $halfThousands = $this->titleInsuranceCalculatorHelper->roundUp2HalfThousands($consideration);

        // CT-31 since the tax rate is specified in % and we are using the tiered level calculator, this uses half
        // thousands as input, therefore we have to multiply the % by one half thousand, 500.

        $tierCosts = array();
        // DC form FP 7/C must accompany deeds to compute taxes or to determine exemptions. The transfer tax and
        // recordation tax increased to 1.45% effective for deeds executed on or after October 1, 2006 where
        // $consideration is $400,000 or more for residential property and for all commercial property. The transfer
        // tax and recordation tax is 1.1% for deeds with consideration less than $400,000 for residential property.
        // For the purpose of determining the tax rate, the deed will be deemed executed on the date the signature
        // of the grantor is notarized. See State Summary for information about tax due on refinancing.
        if ($consideration < $this->rateInfoProperties->getDoubleValue("recordation.dc.tier.threshold")) {
            $tierCosts[] = new TierCost(1234567890, $this->rateInfoProperties->getDoubleValue("recordation.dc.1.rate") * 500);
        } else {
            $tierCosts[] = new TierCost(1234567890, $this->rateInfoProperties->getDoubleValue("recordation.dc.2.rate") * 500);
        }

        return new RecordationTax("Recordation Tax", $this->titleInsuranceCalculatorHelper->calculateTieredCost($halfThousands, $tierCosts, 0.0));
    }

    protected function getStatePropertyTransferTax($consideration) {
        $halfThousands = $this->titleInsuranceCalculatorHelper->roundUp2HalfThousands($consideration);

        // CT-31 since the tax rate is specified in % and we are using the tiered level calculator, this uses half
        // thousands as input, therefore we have to multiply the % by one half thousand, 500.

        $tierCosts = array();
        // DC form FP 7/C must accompany deeds to compute taxes or to determine exemptions. The transfer tax and
        // recordation tax increased to 1.45% effective for deeds executed on or after October 1, 2006 where
        // consideration is $400,000 or more for residential property and for all commercial property. The transfer
        // tax and recordation tax is 1.1% for deeds with consideration less than $400,000 for residential property.
        // For the purpose of determining the tax rate, the deed will be deemed executed on the date the signature
        // of the grantor is notarized. See State Summary for information about tax due on refinancing.
        if ($consideration < $this->rateInfoProperties->getDoubleValue("recordation.transfer.dc.tier.threshold")) {
            $tierCosts[] = new TierCost(1234567890, $this->rateInfoProperties->getDoubleValue("recordation.transfer.dc.1.rate") * 500);
        } else {
            $tierCosts[] = new TierCost(1234567890, $this->rateInfoProperties->getDoubleValue("recordation.transfer.dc.2.rate") * 500);
        }

        return new StatePropertyTransferTax("Transfer Tax", $this->titleInsuranceCalculatorHelper->calculateTieredCost($halfThousands, $tierCosts, 0.0));
    }
}

?>
